<?php 
/**
*  安全中心
*/
class Safetycenter extends CS_Controller
{
    public function _init()
    {
        $this->load->helper(array('common'));
        $this->load->model('user_model', 'user');
    }

    /**
     * 修改那个密码密码 登录密码，支付密码  
     */
    public function selectModifyPass()
    {
        $this->load->view('home/safetycenter/selectModifyPass', $this->data);
    }

    /**
     * 选择登录密码找回方式
     */
    public function selectLoginStrategy()
    {
        $this->data['userInfo'] = $this->user->findOne(['uid' => $this->uid])->row();
        $this->load->view('home/safetycenter/selectLoginStrategy', $this->data);
    }

    /**
     * 记得原有密码
     */
    public function rememberLoginPass()
    {
        $this->load->view('home/safetycenter/rememberLoginPass', $this->data);
    }

    /**
     * 填写原来的密码
     */
    public function rememberLoginPassSubmit()
    {
        $password = $this->input->post('oldPassword');
        if (empty($password) && strlen($password) < 6)  {
            $this->error('home/safetycenter/rememberLoginPass', '', '请输入6-20位的登录密码');
        }

        $result = $this->user->findById($this->uid)->row();
        if ( $result->pw != md5($password) ) {
            $this->error('home/safetycenter/rememberLoginPass', '', '登录密码错误');
        }

        $this->_modifyLoginPass();
    }

    /**
     * 通过短信  修改登录密码
     */
    public function smsLoginStrategy()
    {
        $result = $this->user->findById($this->uid)->row();
        if (!$result->mobile_phone) {
            $this->redirect('home/safetycenter/setFirstMobilePhone');
        }
        $this->data['userInfo'] = $result;
        $this->load->view('home/safetycenter/smsLoginStrategy', $this->data);
    }

    /**
     * 验证 短信效验码  
     */
    public function smsStrategySubmit()
    {
        $captcha = $this->input->post('captcha');
        $endTime = date('Y-m-d H:i:s');
        if (strlen($captcha) != 6) {
            $this->error('home/safetycenter/smsLoginStrategy', '', '请输入字数为6的短信验证码');
        }
        $userInfo = $this->user->findOne(['uid' => $this->uid])->row();
        $result = $this->user_code->findOne(['mobile' => $userInfo->mobile_phone, 'code' => $captcha], 'id, end_time');//, 'end_time' => $endTime
        if ($result->num_rows() <= 0) {
            $this->ajaxJson(false, '手机验证码错误~');
        }
        if ($result->row(0)->end_time < $endTime) {
            $this->ajaxJson(false, '手机验证码超过有效期，请重新获取~');
        }
        if ($result->num_rows() == 0) {
            $this->error('home/safetycenter/smsLoginStrategy', '', '短信验证码不正确');
        }
        $this->_modifyLoginPass();
    }

    /**
     * 修改支付密码
     */
    private function _modifyLoginPass()
    {
        $this->load->view('home/safetycenter/modifyLoginPass', $this->data);
    }

    /**
     * 修改登录密码
     */
    public function modifyLoginPassPost()
    {
        $password = $this->input->post('password');
        $confirmPassword = $this->input->post('confirmPassword');
        $res = $this->user->findById($this->uid)->row();

        if ( strlen($password) < 6 || strlen($confirmPassword) > 20 ) {
            $this->ajaxJson(false, '支付密码字数必须在6至20之间！');
        }

        if ( $password != $confirmPassword ) {
            $this->ajaxJson(false, '2次输入的密码不一致！');
        }
        $result = $this->user->updateUserInfo(array('pw' => md5($password)), $res->uid);
        if ( $result ) {
            $this->ajaxJson(true, site_url('home/ucenter/baseInfoSet'));
        } else {
            $this->ajaxJson(false, '网络错误~');
        }
    }

    /**
     * 选择支付密码找回方式
     */
    public function selectPaymentStrategy()
    {
        $result = $this->user->findById($this->uid)->row();
        if (!$result->payment_password) {
            $this->redirect('home/safetycenter/setFirstPaymentPass');
        }
        if (!$result->mobile_phone) {
            $this->redirect('home/safetycenter/setFirstMobilePhone');
        }
        $this->data['userInfo'] = $result;
        $this->load->view('home/safetycenter/selectPaymentStrategy', $this->data);
    }

    /**
     * 第一次设置支付密码
     */
    public function setFirstPaymentPass()
    {
        $result = $this->user->findById($this->uid)->row();
        if ($result->payment_password ) {
            $this->redirect('home/safetycenter/selectPaymentStrategy');
        } else {
            $this->_modifyPaymentPass();
        }
    }

    /**
     * 记得原有支付密码密码 
     */
    public function rememberPaymentPass()
    {
        $this->load->view('home/safetycenter/rememberPaymentPass', $this->data);
    }

    /**
     * 填写原来的密码
     */
    public function rememberPaymentPassSubmit()
    {
        $password = $this->input->post('oldPassword');
        if (empty($password) && strlen($password) < 6) {
            $this->error('home/safetycenter/rememberPaymentPass', '', '请输入6位以上的支付密码');
        }
        $result = $this->user->findById($this->uid)->row();
        if ( $result->payment_password != md5($password) ) {
            $this->error('home/safetycenter/rememberPaymentPass', '', '支付密码错误');
        }

        $this->_modifyPaymentPass();
    }

    /**
     * 通过短信  修改支付密码
     */
    public function smsStrategy()
    {
        $result = $this->user->findById($this->uid)->row();
        if (!$result->mobile_phone ) {
            $this->redirect('home/safetycenter/selectPaymentStrategy');
        }
        $this->data['userInfo'] = $result;
        $this->load->view('home/safetycenter/smsStrategy', $this->data);
    }

    /**
     * 验证 短信效验码  
     */
    public function smsStrategyPost()
    {
        $captcha = $this->input->post('captcha');
        $endTime = date('Y-m-d H:i:s');
        if ( strlen($captcha) != 6 ) {
            $this->error('home/safetycenter/smsStrategy', '', '请输入字数为6的短信验证码');
        }
        $userInfo = $this->user->findById($this->uid)->row();
        $result = $this->user_code->findOne(['mobile' => $userInfo->mobile_phone, 'code' => $captcha], 'id, end_time');//, 'end_time' => $endTime
        if ($result->num_rows() <= 0) {
            $this->ajaxJson(false, '手机验证码错误~');
        }
        if ($result->row(0)->end_time < $endTime) {
            $this->ajaxJson(false, '手机验证码超过有效期，请重新获取~');
        }
        if ( $result->num_rows() == 0 ) {
            $this->error('home/safetycenter/smsStrategy', '', '短信验证码不正确');
        }
        $this->_modifyPaymentPass();
    }

    /**
     * 修改支付密码
     */
    private function _modifyPaymentPass()
    {
        $this->load->view('home/safetycenter/modifyPaymentPass', $this->data);
    }

    /**
     * 修改支付密码
     */
    public function modifyPaymentPassPost()
    {
        $password = $this->input->post('password');
        $confirmPassword = $this->input->post('confirmPassword');
        $res = $this->user->findById($this->uid)->row();

        if (strlen($password) < 6 || strlen($confirmPassword) > 20) {
            $this->ajaxJson(false, '支付密码字数必须在6至20之间！');
        }
        if ( $password != $confirmPassword ) {
            $this->ajaxJson(false,'2次输入的密码不一致！');
        }
        $result = $this->user->updateUserInfo(['payment_password' => md5($password)], $res->uid);
        if ($result) {
            $this->ajaxJson(true, site_url('home/ucenter/baseInfoSet'));
        } else {
            $this->ajaxJson(false, '网络错误~');
        }
    }

    /**
     * 发送短信效验码
     */
    public function ajaxSendSms()
    {
        $phone = $this->input->post('mobilePhone');

        if (!valid_mobile($phone)) {
            $this->ajaxJson(false, '手机号码格式有误');
        }

        return $this->sendCommonSms($phone, 'SMS_135430021');
    }

    /**
     * 选择修改绑定手机号
     */
    public function selectMobilePhone()
    {
        $result = $this->user->findById($this->uid)->row();
        if (empty($result->mobile_phone)) {
            $this->redirect('home/safetycenter/setFirstMobilePhone');
        }
        $this->data['userInfo'] = $result;
        $this->load->view('home/safetycenter/selectMobilePhone', $this->data);
    }

    /**
     * 第一次绑定手机号
     */
    public function setFirstMobilePhone()
    {
        $result = $this->user->findById($this->uid)->row();
        if (!empty($result->mobile_phone)) {
            $this->redirect('home/safetycenter/selectMobilePhone');
        } else {
            $this->load->view('home/safetycenter/setFirstMobilePhone', $this->data);
        }
    }

    /**
     * 验证需要修改的手机号
     */
    public function validateMobilePhone()
    {
        $mobile = $this->input->post('mobile_phone');
        $code   = $this->input->post('captcha');
        $status = $this->validateSmsInfo($mobile, $code);
        if (!$status['status']) {
            $this->ajaxJson($status);
        }
        $res = $this->user->findOne(['uid' => $this->uid, 'mobile_phone' => $mobile]);
        if ($res->num_rows() <= 0) { //修改手机号码
            $this->ajaxJson(false, '账号信息有误~');
        }
        $this->ajaxJson(true, site_url('home/safetycenter/modifyMobilePhone'));
    }

    /**
     * 修改绑定手机号页面
     */
    public function modifyMobilePhone()
    {
        $this->load->view('home/safetycenter/modifyMobilePhone', $this->data);
    }

    /**
     * 修改绑定手机号或第一次绑定手机号操作
     */
    public function modifyMobilePhonePost()
    {
        $mobile = $this->input->post('mobile_phone');
        $code   = $this->input->post('captcha');

        $status = $this->validateSmsInfo($mobile, $code);
        if (!$status['status']) {
            $this->ajaxJson($status);
        }
        $res = $this->user->findOne(['uid' => $this->uid]);
        if ($res->num_rows() <= 0) { //修改手机号码
            $this->ajaxJson(false, '账号信息有误~');
        }
        $user = $res->row(0);
        if (!empty($user->mobile_phone) && $mobile == $user->mobile_phone) {
            $this->ajaxJson(false, '与当前绑定的手机号相同~');
        }
        if (empty($user->mobile_phone)) { //说明第一次绑定手机
            $modify = $this->user->update(['mobile_phone' => $mobile], ['uid' => $this->uid]);
        } else {
            $newUser = $this->user->findOne(['mobile_phone' => $mobile]);
            if ($newUser->num_rows() > 0) {
                $this->ajaxJson(false, '手机号码已经存在，不能重复绑定~');
            }
            $modify = $this->user->update(['mobile_phone' => $mobile], ['uid' => $this->uid]);
        }
        if ($modify) {
            $this->ajaxJson(true, site_url('home/ucenter/baseInfoSet'));
        }
        $this->ajaxJson(false, '网络错误~');
    }
}